//
//  ADScrollView.m
//  美物说
//
//  Created by J on 15/8/18.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "ADScrollView.h"
#import "Header.h"

#import "ThirdNaviSearchController.h"


@interface ADScrollView ()<UIScrollViewDelegate>

{
    
    NSMutableArray* _mutableArray;
    UIPageControl* _pageControl;
    
    
    
}
@property (nonatomic, strong) UIScrollView *adScrollView;

@property(nonatomic,strong)NSMutableArray* NEWArray;
@property(nonatomic,strong)NSTimer       * timer;

@end

@implementation ADScrollView


- (instancetype)initWithFrame:(CGRect)frame andImageArr:(NSMutableArray *)image_url andValueArray:(NSMutableArray*)valueArray;
{

    
    [_adScrollView performSelector:@selector(removeFromSuperview)];
    
    
    if (self=[super initWithFrame:frame]) {
        
        
//        JZJLog(@"_mutableArray:::%@",image_url);

        
        
        if (!valueArray.count) {
            
            JZJLog(@"滚动广告数组为空");
            return self;
        }
        _valueArray=[NSMutableArray array];
        _NEWArray=[NSMutableArray array];
        
        
        [_valueArray addObject:[valueArray lastObject]];
        [_NEWArray addObject:[image_url lastObject]];
        
        
        
        [_valueArray addObjectsFromArray:valueArray];

        [_NEWArray addObjectsFromArray:image_url];


        [_NEWArray addObject:[image_url firstObject]];

        [_valueArray addObject:[valueArray firstObject]];

        
        
        

        
        
        
        //
        
        _mutableArray=[NSMutableArray array];

    
        
        _adScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, frame.size.height)];
        _adScrollView.pagingEnabled=YES;
        _adScrollView.bounces=NO;
        _adScrollView.showsVerticalScrollIndicator=NO;
        
        _adScrollView.showsHorizontalScrollIndicator=NO;

        
        _adScrollView.contentSize = CGSizeMake(_NEWArray.count*SCREEN_WIDTH, frame.size.height);
        
//        _adScrollView.contentOffset=CGPointMake(SCREEN_WIDTH, 0);
        
        
        [self addSubview:_adScrollView];
        _adScrollView.delegate = self;

        
        
        
        
        //        通过遍历newImages创建UIImageView对象，并且添加到_adScrollView上面
        [_NEWArray enumerateObjectsUsingBlock:^(NSString *url, NSUInteger idx, BOOL *stop) {
            
            
            //             创建imageView对象
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(idx*frame.size.width, 0, SCREEN_WIDTH, frame.size.height)];
            //            设置image属性

            
            [imageView  sd_setImageWithURL:[NSURL URLWithString:url]];

            [_adScrollView addSubview:imageView];
            
//            JZJLog(@"%zd",idx);
            
            imageView.userInteractionEnabled = YES;
            
            imageView.tag=idx;
            Model* model=_valueArray[idx];
            imageView.restorationIdentifier=[NSString stringWithFormat:@"%@",model.value];
            
            UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
            
            
            [imageView addGestureRecognizer:tap];
            
            
            
        }];
        
        
        
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.numberOfPages = image_url.count;
        _pageControl.currentPageIndicatorTintColor=[UIColor blackColor];
        _pageControl.pageIndicatorTintColor=[UIColor grayColor];

        
        CGSize size = [_pageControl sizeForNumberOfPages:image_url.count];
        _pageControl.frame = CGRectMake((frame.size.width - size.width )/2.0, frame.size.height - size.height-3, size.width, size.height);

        [self addSubview:_pageControl];
        
    
//        [_pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
//      
        
       NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(run:) userInfo:nil repeats:YES];
        
        self.timer = timer;
        

        
             
    }


    return self;
}



//滚图的动画效果
-(void)pageTurn:(UIPageControl *)aPageControl{
    int whichPage = aPageControl.currentPage;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [_adScrollView setContentOffset:CGPointMake(320.0f * whichPage, 0.0f) animated:YES];
    [UIView commitAnimations];
}

- (void)run:(NSTimer*)time
{
            [UIView animateWithDuration:1.0 animations:^{
              _adScrollView.contentOffset=CGPointMake(_adScrollView.contentOffset.x+SCREEN_WIDTH, 0);
            }];
    NSInteger page = _adScrollView.contentOffset.x/(NSInteger)self.frame.size.width;
    
    if (page==0||page==_NEWArray.count-1) {
        if (page==0) {
          _adScrollView.contentOffset=CGPointMake((_NEWArray.count-2)*SCREEN_WIDTH, 0);
            page=_NEWArray.count-1;
            
        }else if(page==_NEWArray.count-1)
        {
            _adScrollView.contentOffset=CGPointMake(SCREEN_WIDTH, 0);
            page=1;
        }
    }
    
    
    _pageControl.currentPage = page-1;
    

}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.timer.fireDate = [NSDate distantFuture];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];

}


- (void)tap:(UITapGestureRecognizer*)tap
{

    
    NSInteger length=tap.view.restorationIdentifier.length;

    
    if (length==4) {

        Model* model=_valueArray[tap.view.tag];
        

        
        WebViewController* vc=[[WebViewController alloc]init];
        vc.model=model;
        vc.hidesBottomBarWhenPushed=YES;
        
        
        
        [self.delegate.navigationController pushViewController:vc animated:YES];

    
        
    }else{
    
    
        ThirdNaviSearchController* vc=[[ThirdNaviSearchController alloc]init];
        Model* model0=_valueArray[tap.view.tag];

        Model* model=[[Model alloc]init];
        
        model.id=model0.value;
        vc.model=model;


        [self.delegate.navigationController pushViewController:vc animated:YES];

        
    
    
    }
    
    
    

}


#pragma mark --UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    
    NSInteger page = scrollView.contentOffset.x/(NSInteger)self.frame.size.width;
    
    if (page==0||page==_NEWArray.count-1) {
        
        
        if (page==0) {
            
            
            _adScrollView.contentOffset=CGPointMake((_NEWArray.count-2)*SCREEN_WIDTH, 0);
            page=_NEWArray.count-1;
            
        }else if(page==_NEWArray.count-1)
        {
            
            
            _adScrollView.contentOffset=CGPointMake(SCREEN_WIDTH, 0);
            page=1;

            
            
        }
        
        
    }
    
  
    
//    JZJLog(@"%zd",page);
    
    
       //    让controll同步
    _pageControl.currentPage = page-1;
    
}




@end
